package src.leetcode.tree.simple_0257;

import src.tree.TreeNode;

import java.util.*;

public class binaryTreePaths {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,null,5));
        TreeNode root = TreeNode.createTree(list);

        Solution solution = new Solution();
        System.out.println(solution.binaryTreePaths(root));
    }
}


class Solution {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new LinkedList<>();
        constructPaths(root, "", res);
        return res;
    }

    private void constructPaths(TreeNode root, String s, List<String> res) {
        if(root != null) {
            StringBuffer sb = new StringBuffer(s);
            sb.append(root.val);
            if(root.left == null && root.right == null) {
                res.add(sb.toString());
            }else {
                sb.append("->");
                constructPaths(root.left, sb.toString(), res);
                constructPaths(root.right, sb.toString(), res);
            }
        }
    }


//    public List<String> binaryTreePaths(TreeNode root) {
//        List<String> res = new ArrayList<>();
//        if(root == null) return res;
//
//        Deque<TreeNode> stack = new LinkedList<>();
//        stack.addFirst(root);
//        while(!stack.isEmpty()){
//            TreeNode node = stack.peekFirst();
//            while (node.left != null) {
//                stack.addFirst(node.left);
//                node = node.left;
//            }
//
//            if(node.right == null){
//                StringBuilder sb = new StringBuilder();
//                for(TreeNode n : stack){
//                    sb.append(n.val);
//                    sb.append("->");
//                }
//                sb.append(node.val);
//                res.add(sb.toString());
//
//                //res.add(stack.stream().map(n -> n.val).collect(Collectors.joining("->")) + "->" + node.val);
//            }else {
//                stack.addFirst(node.right);
//            }
//        }
//
//
//        return res;
//
//    }

}